
<template>
  <!-- 我的历史 -->

  <div class="myHistory">
    <myheader :title="title" rightBtn="主页" @updataInfo="goHome"></myheader>

    <div class="serarch">
      <van-search v-model="searchValue" show-action placeholder="请输入搜索关键词" @search="onSearch" />
    </div>

    <div v-if="!empty">
      <vote-single
        v-if="type==1"
        @getVotes="handelChanges"
        :total="total"
        :vote="vote"
        :currentPage="currentPage"
        :pageCount="pageCount"
      ></vote-single>

      <vote-single
        v-if="type==2"
        @getVotes="handelChanges"
        :total="total"
        :vote="myvote"
        :currentPage="currentPage"
        :pageCount="pageCount"
      ></vote-single>

      <lostFond
        v-if="type==3"
        @getLost="handelChanges"
        :pageSize="pageSize"
        :total="total"
        :lostFound="lostFound"
        :currentPage="currentPage"
        :pageCount="pageCount"
      ></lostFond>

      <searchNode
        v-if="type==4"
        @getSearchNotic="handelChanges"
        :pageSize="pageSize"
        :total="total"
        :searchNotice="searchNode"
        :currentPage="currentPage"
        :pageCount="pageCount"
      ></searchNode>

      <chart v-if="type==5" :chartLsit="chartLsit">
        <el-pagination
          class="pagination"
          background
          style="margin-top: 1rem"
          layout="total, prev, pager, next"
          @current-change="handelChanges"
          :currentPage="currentPage"
          :page-size="pageSize"
          :total="total"
        ></el-pagination>
      </chart>

      <van-tabs v-if="type==6" :active="active" @change="selectedType">
        <van-tab title="我的借用物品">
          <borrowItem
            v-if="type==6 && active==0"
            @getBorrow="handelChanges"
            :pageSize="pageSize"
            :total="total"
            :borrow="borrowList"
            :currentPage="currentPage"
            :pageCount="pageCount"
            :active="active"
          ></borrowItem>
        </van-tab>
        <van-tab title="我的发布的资源">
          <borroRecord
            v-if="type==6 && active==1"
            @getBorrow="handelChanges"
            :pageSize="pageSize"
            :total="total"
            :borrow="borrowList"
            :currentPage="currentPage"
            :pageCount="pageCount"
            :active="active"
          ></borroRecord>
        </van-tab>
      </van-tabs>

      <timeHistory :repairHistoryList="repairHistoryList"></timeHistory>

      <goodsHistory v-if="type==8" :buygoods="buygoods">
        <el-pagination
          class="pagination"
          background
          style="margin-top: 1rem"
          layout="total, prev, pager, next"
          @current-change="handelChanges"
          :currentPage="currentPage"
          :page-size="pageSize"
          :total="total"
        ></el-pagination>
      </goodsHistory>

      <addSharelife v-if="type==9">
        <template #showmylife>
          <div>
            <van-popup
              :show="showLifes"
              @click-close-icon="showLifes=false"
              @click-overlay="showLifes=false"
              closeable
              position="bottom"
              style="height:60%;padding:35px 5px 0 5px;"
            >
              <div v-if="showLifesList.length>0">
                <div>
                  <van-card
                    style="paddiing-top:30px;"
                    :desc="item.title"
                    :title="item.content"
                    v-for="(item, index) in showLifesList"
                    :key="index"
                    @click="goDetail(item.id)"
                  >
                    <template #thumb>
                      <van-image :src="item.imgList[0]" class="swip_img">
                        <template v-slot:loading>
                          <van-loading size="20px" vertical>加载中...</van-loading>
                        </template>
                      </van-image>
                    </template>
                  </van-card>
                </div>
              </div>
              <van-empty v-else description="您还没有发表任何动态呦╮(╯▽╰)╭！" />
            </van-popup>
          </div>
        </template>
      </addSharelife>
    </div>

    <van-empty v-else image="error" description="暂无此内容！" />
  </div>
</template>

<script>
import myheader from '../../comon/myheader.vue'

import voteSingle from '../../comon/voteSingle.vue'
import lostFond from '../../comon/lostFond.vue'
import searchNode from '../../comon/searchNode.vue'
import borrowItem from '../../comon/borrowItem.vue'
import borroRecord from '../../comon/borroRecord.vue'
import timeHistory from '../../comon/timeHistory.vue'
import goodsHistory from '../../comon/goodsHistory.vue'
import addSharelife from '../../components/index/more/addSharelife.vue'
import chart from '../../comon/chart.vue'
export default {
  components: {
    myheader,
    voteSingle,
    lostFond,
    searchNode,
    borrowItem,
    chart,
    borroRecord,
    timeHistory,
    goodsHistory,
    addSharelife,
  },
  data() {
    return {
      type: this.$route.query.type,
      title: this.$route.query.title,
      userId: this.$store.getters.getUserInfo.id,
      searchValue: '',
      empty: false,
      currentPage: 1,
      pageSize: 4,
      pageCount: 1,
      total: 1,
      vote: [], // 我的投票
      myvote: [], //我发起的投票
      lostFound: [], // 我丢失的物品
      searchNode: [], //我捡到的物品
      borrowList: [], //我借用社区的物品
      chartLsit: [], // 我要租出的物品
      active: 0, // 激活页面
      buygoods: [], // 我买的东西
      repairHistoryList: [], //我的维修记录
      showLifes: false, // 显示我的分享生活记录
      showLifesList: [], // 显示我的分享生活记录列表
    }
  },
  mounted() {
    if (this.type == 1) {
      this.getvote()
    } else if (this.type == 2) {
      this.getMyvote()
    } else if (this.type == 3) {
      this.getlostFond()
    } else if (this.type == 4) {
      this.getFoundSearch()
    } else if (this.type == 5) {
      this.getYield()
    } else if (this.type == 6) {
      this.getBorrow()
    } else if (this.type == 7) {
      this.repair()
    } else if (this.type == 8) {
      this.getBuygoods()
    }
  },
  methods: {
    // 获取我的投票
    async getvote() {
      const res = await this.$axios.get('/vote/listByCastVote', {
        params: {
          current: this.currentPage,
          size: this.pageSize,
          userId: this.$store.getters.getUserInfo.id,
        },
      })

      if (res.data.code == 200) {
        this.vote = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
      }
    },
    async voteSearch() {
      const res = await this.$axios.get(
        '/vote/listByCastVote?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&title=' +
          this.searchValue +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.vote = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
        this.searchValue = ''

        if (res.data.data.total == 0) {
          this.empty = true
        } else {
          this.empty = false
        }
      }
    },
    // 我发起的投票
    async getMyvote() {
      const res = await this.$axios.get(
        '/vote/listByUserId?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&userId=' +
          this.userId
      )

      if (res.data.code == 200) {
        this.myvote = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
      }
    },
    async myVoteSearch() {
      const res = await this.$axios.get(
        '/vote/listByUserId?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&title=' +
          this.searchValue +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.myvote = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
        this.searchValue = ''

        if (res.data.data.total == 0) {
          this.empty = true
        } else {
          this.empty = false
        }
      }
    },
    // 我的丢失物品
    async getlostFond() {
      const res = await this.$axios.get(
        '/item/lost/list?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.lostFound = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
      }
    },
    async lostSearch() {
      const res = await this.$axios.get(
        '/item/lost/list?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&title=' +
          this.searchValue +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.lostFound = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
        this.searchValue = ''
        if (res.data.data.total == 0) {
          this.empty = true
        } else {
          this.empty = false
        }
      }
    }, // 我捡到的物品
    async getFoundSearch() {
      const res = await this.$axios.get(
        '/item/find/list?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.searchNode = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
      }
    },
    async foundSearch() {
      const res = await this.$axios.get(
        '/item/find/list?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&title=' +
          this.searchValue +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.searchNode = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
        this.searchValue = ''
        if (res.data.data.total == 0) {
          this.empty = true
        } else {
          this.empty = false
        }
      }
    }, // 我的转让物品
    async getYield() {
      const res = await this.$axios.get(
        '/goods/listByUserId?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.chartLsit = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
      }
    },
    async yieldSearch() {
      const res = await this.$axios.get(
        '/goods/listByUserId??current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&title=' +
          this.searchValue +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.chartLsit = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
        this.searchValue = ''
        if (this.chartLsit.length == 0) {
          this.empty = true
        } else {
          this.empty = false
        }
      }
    },
    // 我的社区借用记录
    async getBorrow() {
      const res = await this.$axios.get(
        '/item/borrow/listByLease?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.borrowList = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
      }
      console.log(this.borrowList)
    },
    async borrowSearch() {
      const res = await this.$axios.get(
        '/item/borrow/listByLease?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&title=' +
          this.searchValue +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.borrowList = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
        this.searchValue = ''
        if (this.borrowList.length == 0) {
          this.empty = true
        } else {
          this.empty = false
        }
      }
    },
    // 我在社区发布的资源
    async getMyboorow() {
      const res = await this.$axios.get(
        '/item/borrow/listByUserId?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.borrowList = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
      }
      console.log(this.borrowList)
    },
    async myBorrowSearch() {
      const res = await this.$axios.get(
        '/item/borrow/listByUserId?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&title=' +
          this.searchValue +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.borrowList = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
        this.searchValue = ''
        if (this.borrowList.length == 0) {
          this.empty = true
        } else {
          this.empty = false
        }
      }
    },
    // 我的维修记录
    async repair() {
      const res = await this.$axios.get(
        '/property/repair/showByUserId?userId=' +
          this.$store.getters.getUserInfo.id
      )
      if ((res.data.code = 200)) {
        this.repairHistoryList = res.data.data.records
        this.empty = false
      } else {
        this.empty = true
      }
      console.log(this.repairHistoryList)
    },
    async repairSearch() {
      const res = await this.$axios.get(
        '/property/repair/showByUserId?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&describe=' +
          this.searchValue +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.repairHistoryList = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
        this.searchValue = ''
        if (this.repairHistoryList.length == 0) {
          this.empty = true
        } else {
          this.empty = false
        }
      }
    },
    // 我的购买记录
    async getBuygoods() {
      const res = await this.$axios.get(
        '/order/listByUserId?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.buygoods = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
      }
      console.log(this.buygoods)
    },
    async buygoodsSearch() {
      const res = await this.$axios.get(
        '/order/listByUserId?current=' +
          this.currentPage +
          '&size=' +
          this.pageSize +
          '&subject=' +
          this.searchValue +
          '&userId=' +
          this.userId
      )
      if (res.data.code == 200) {
        this.buygoods = res.data.data.records
        this.total = res.data.data.total
        this.pageCount = res.data.data.pages
        this.searchValue = ''
        if (this.buygoods.length == 0) {
          this.empty = true
        } else {
          this.empty = false
        }
      }
    },

    // 添加分享动态

    selectedType(vals) {
      this.active = vals
      if (this.active == 1) {
        this.getMyboorow()
      } else if (this.active == 0) {
        this.getBorrow()
      }
    },
    async myLifeSearch() {
      const res = await this.$axios.get(
        '/dynamic/list?title=' + this.searchValue
      )
      if (res.data.code == 200) {
        let arr = res.data.data.records
        for (let i = 0; i < arr.length; i++) {
          if (this.$store.getters.getUserInfo.id == arr[i].user.userId) {
            arr.splice(i, 1)
          }
        }
        this.showLifesList = arr
        this.showLifes = true
      }
      this.searchValue = ''
    },
    goDetail(id) {
      this.$router.push('/shareLifeDetails/' + id)
    },
    // 搜索
    onSearch() {
      if (this.type == 1) {
        this.voteSearch()
      } else if (this.type == 2) {
        this.myVoteSearch()
      } else if (this.type == 3) {
        this.lostSearch()
      } else if (this.type == 4) {
        this.foundSearch()
      } else if (this.type == 5) {
        this.yieldSearch()
      } else if (this.type == 6) {
        if (this.active == 1) {
          this.myBorrowSearch()
        } else {
          this.borrowSearch()
        }
      } else if (this.type == 7) {
        this.repairSearch()
      } else if (this.type == 8) {
        this.buygoodsSearch()
      } else if (this.type == 9) {
        this.myLifeSearch()
      }
    },
    handelChanges(val) {
      this.currentPage = val
      if (this.type == 1) {
        this.getvote()
      } else if (this.type == 2) {
        this.getMyvote()
      } else if (this.type == 3) {
        this.getlostFond()
      } else if (this.type == 4) {
        this.getFoundSearch()
      } else if (this.type == 5) {
        this.getYield()
      } else if (this.type == 6) {
        if (this.active == 1) {
          this.getMyboorow()
        } else {
          this.getBorrow()
        }
      } else if (this.type == 7) {
        this.repair()
      } else if (this.type == 8) {
        this.getBuygoods()
      }
    },
    // 跳转到home页面
    goHome() {
      this.$router.push('/home')
    },
  },
}
</script>

<style  scoped>
.wisdomAlertor,
.serarch {
  margin-top: 46px;
}
</style>>
 